package org.bemuv;

import java.util.List;

import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.parse.SignUpCallback;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddShopActivity extends Activity {
	
	private Button btn_fishop;
	private EditText txt_nameshop;
	private EditText txt_adress;
	private EditText txt_phone;
	private String current="Nada";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_shop);
		btn_fishop = (Button) findViewById(R.id.btn_finishop);

		// Asigna escuchas
		btn_fishop.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				addshop();
				
			}
		});
	}
	
	public void addshop() {
		boolean error = false;
    	final ParseObject user = new ParseUser();
		txt_nameshop = (EditText) findViewById(R.id.txt_nameshop);
		txt_adress = (EditText) findViewById(R.id.txt_adress);
		txt_phone = (EditText) findViewById(R.id.txt_phone);
		

		if (txt_nameshop.getText().toString().length() == 0) {
			txt_nameshop.setError("Requerido");
			error = true;
		}
		if (txt_adress.getText().toString().length() == 0) {
			txt_adress.setError("Requerida");
			error = true;
		}
		if (txt_phone.getText().toString().length() == 0) {
			txt_phone.setError("Requerido");
			error = true;
		}


	
		if (!error) {   //If todo esta bien ---------
	
			
			ParseUser currentUser = ParseUser.getCurrentUser();
			current = (String) currentUser.get("username");


			ParseObject addShop = new ParseObject("userShop");
			addShop.put("phone", txt_phone.getText().toString());
			addShop.put("addressShop", txt_adress.getText().toString());
			addShop.put("shopName", txt_nameshop.getText().toString());
			addShop.put("userID", current); //guardo el ID del usuario 
			addShop.put("userType", "sh");//tipo de usuario Shop
			//FALTA AGREGAR LAS COORDENADAS DEL GPS
			
			addShop.saveInBackground(new SaveCallback() {
	            public void done(ParseException e) {
	                if (e == null) {
	                	Toast.makeText(getApplicationContext(),
	        					"Se guardo satisfactoriamente", Toast.LENGTH_SHORT).show();
	                	Intent i = new Intent(getApplicationContext(),
	        					AddItemActivity.class);
	        			startActivity(i);

	                } else {
	                 
	                }
	            }
	        });

	}  	

		} //If todo esta bien ---------

	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_add_shop, menu);
		return true;
	}

}
